import { WinsleiLogManager } from '@winslei/winsleilog'
import image from '@ohos.multimedia.image';
import util from '@ohos.util';
import data from '@ohos.telephony.data';

const TAG = 'ImageUtil'

export class ImageUtil {
  constructor() {
    WinsleiLogManager.hilogIWithTag(TAG, `constructor`)
  }

  // base64ToPixelMap
  base64ToPixelMap(base64: string): Promise<image.PixelMap> {
    WinsleiLogManager.hilogIWithTag(TAG, `base64ToPixelMap enter. base64: ${JSON.stringify(base64)}`)

    const reg = new RegExp('data:image/\\w+;base64,')
    const base64Str = base64.replace(reg, '')

    let base64Helper = new util.Base64Helper();
    WinsleiLogManager.hilogIWithTag(TAG, `base64ToPixelMap, base64Helper: ${JSON.stringify(base64Helper)}`)

    let uint8Array = base64Helper.decodeSync(base64Str)
    WinsleiLogManager.hilogIWithTag(TAG, `base64ToPixelMap, uint8Array: ${JSON.stringify(uint8Array)}`)

    let arrayBuffer = uint8Array.buffer
    let imageSource = image.createImageSource(arrayBuffer)
    let opts: image.DecodingOptions = { editable: false }

    return imageSource.createPixelMap(opts);
  }

}